<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CAD_Doc: Quickstart: Building with CMake</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">CAD_Doc<span id="projectnumber">&#160;1.0</span>
   </div>
   <div id="projectbrief">cad synopsis</div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('md__build_2__deps_2googletest-src_2docs_2quickstart-cmake.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Quickstart: Building with CMake</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md238"></a></p>
<p>This tutorial aims to get you up and running with GoogleTest using CMake. If you're using GoogleTest for the first time or need a refresher, we recommend this tutorial as a starting point. If your project uses Bazel, see the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2quickstart-bazel.html">Quickstart for Bazel</a> instead.</p>
<h1><a class="anchor" id="autotoc_md239"></a>
Prerequisites</h1>
<p>To complete this tutorial, you'll need:</p>
<ul>
<li>A compatible operating system (e.g. Linux, macOS, Windows).</li>
<li>A compatible C++ compiler that supports at least C++14.</li>
<li><a href="https://cmake.org/">CMake</a> and a compatible build tool for building the project.<ul>
<li>Compatible build tools include <a href="https://www.gnu.org/software/make/">Make</a>, <a href="https://ninja-build.org/">Ninja</a>, and others - see <a href="https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html">CMake Generators</a> for more information.</li>
</ul>
</li>
</ul>
<p>See <a class="el" href="md__build_2__deps_2googletest-src_2docs_2platforms.html">Supported Platforms</a> for more information about platforms compatible with GoogleTest.</p>
<p>If you don't already have CMake installed, see the <a href="https://cmake.org/install">CMake installation guide</a>.</p>
<p>{: .callout .note} Note: The terminal commands in this tutorial show a Unix shell prompt, but the commands work on the Windows command line as well.</p>
<h1><a class="anchor" id="autotoc_md240"></a>
Set up a project</h1>
<p>CMake uses a file named <code>CMakeLists.txt</code> to configure the build system for a project. You'll use this file to set up your project and declare a dependency on GoogleTest.</p>
<p>First, create a directory for your project:</p>
<div class="fragment"><div class="line">$ mkdir my_project &amp;&amp; cd my_project</div>
</div><!-- fragment --><p>Next, you'll create the <code>CMakeLists.txt</code> file and declare a dependency on GoogleTest. There are many ways to express dependencies in the CMake ecosystem; in this quickstart, you'll use the <a href="https://cmake.org/cmake/help/latest/module/FetchContent.html"><code>FetchContent</code> CMake module</a>. To do this, in your project directory (<code>my_project</code>), create a file named <code>CMakeLists.txt</code> with the following contents:</p>
<div class="fragment"><div class="line">cmake_minimum_required(VERSION 3.14)</div>
<div class="line">project(my_project)</div>
<div class="line"> </div>
<div class="line"># GoogleTest requires at least C++14</div>
<div class="line">set(CMAKE_CXX_STANDARD 14)</div>
<div class="line"> </div>
<div class="line">include(FetchContent)</div>
<div class="line">FetchContent_Declare(</div>
<div class="line">  googletest</div>
<div class="line">  GIT_REPOSITORY https://github.com/google/googletest.git</div>
<div class="line">  GIT_TAG release-1.12.1</div>
<div class="line">)</div>
<div class="line"># For Windows: Prevent overriding the parent project&#39;s compiler/linker settings</div>
<div class="line">set(gtest_force_shared_crt ON CACHE BOOL &quot;&quot; FORCE)</div>
<div class="line">FetchContent_MakeAvailable(googletest)</div>
</div><!-- fragment --><p>The above configuration declares a dependency on GoogleTest which is downloaded from GitHub. In the above example, <code>609281088cfefc76f9d0ce82e1ff6c30cc3591e5</code> is the Git commit hash of the GoogleTest version to use; we recommend updating the hash often to point to the latest version.</p>
<p>For more information about how to create <code>CMakeLists.txt</code> files, see the <a href="https://cmake.org/cmake/help/latest/guide/tutorial/index.html">CMake Tutorial</a>.</p>
<h1><a class="anchor" id="autotoc_md241"></a>
Create and run a binary</h1>
<p>With GoogleTest declared as a dependency, you can use GoogleTest code within your own project.</p>
<p>As an example, create a file named <code>hello_test.cc</code> in your <code>my_project</code> directory with the following contents:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;gtest/gtest.h&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Demonstrate some basic assertions.</span></div>
<div class="line">TEST(HelloTest, BasicAssertions) {</div>
<div class="line">  <span class="comment">// Expect two strings not to be equal.</span></div>
<div class="line">  EXPECT_STRNE(<span class="stringliteral">&quot;hello&quot;</span>, <span class="stringliteral">&quot;world&quot;</span>);</div>
<div class="line">  <span class="comment">// Expect equality.</span></div>
<div class="line">  EXPECT_EQ(7 * 6, 42);</div>
<div class="line">}</div>
</div><!-- fragment --><p>GoogleTest provides <a href="primer.md#assertions">assertions</a> that you use to test the behavior of your code. The above sample includes the main GoogleTest header file and demonstrates some basic assertions.</p>
<p>To build the code, add the following to the end of your <code>CMakeLists.txt</code> file:</p>
<div class="fragment"><div class="line">enable_testing()</div>
<div class="line"> </div>
<div class="line">add_executable(</div>
<div class="line">  hello_test</div>
<div class="line">  hello_test.cc</div>
<div class="line">)</div>
<div class="line">target_link_libraries(</div>
<div class="line">  hello_test</div>
<div class="line">  GTest::gtest_main</div>
<div class="line">)</div>
<div class="line"> </div>
<div class="line">include(GoogleTest)</div>
<div class="line">gtest_discover_tests(hello_test)</div>
</div><!-- fragment --><p>The above configuration enables testing in CMake, declares the C++ test binary you want to build (<code>hello_test</code>), and links it to GoogleTest (<code>gtest_main</code>). The last two lines enable CMake's test runner to discover the tests included in the binary, using the <a href="https://cmake.org/cmake/help/git-stage/module/GoogleTest.html"><code>GoogleTest</code> CMake module</a>.</p>
<p>Now you can build and run your test:</p>
<pre>
<b>my_project$ cmake -S . -B build</b>
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
...
-- Build files have been written to: .../my_project/build

<b>my_project$ cmake --build build</b>
Scanning dependencies of target gtest
...
[100%] Built target gmock_main

<b>my_project$ cd build &amp;&amp; ctest</b>
Test project .../my_project/build
    Start 1: HelloTest.BasicAssertions
1/1 Test #1: HelloTest.BasicAssertions ........   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec
</pre><p>Congratulations! You've successfully built and run a test binary using GoogleTest.</p>
<h1><a class="anchor" id="autotoc_md242"></a>
Next steps</h1>
<ul>
<li><a class="el" href="md__build_2__deps_2googletest-src_2docs_2primer.html">Check out the Primer</a> to start learning how to write simple tests.</li>
<li><a class="el" href="md__build_2__deps_2googletest-src_2docs_2samples.html">See the code samples</a> for more examples showing how to use a variety of GoogleTest features. </li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
  </ul>
</div>
</body>
</html>
